package com.leyou.common.advice;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.ExceptionResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author huangxin
 * @date 2019/4/18-17:31
 * 通用异常通知
 */
@ControllerAdvice//此注解会自动拦截所有的controller
public class CommonExceptionHandler {
    @ExceptionHandler({LyException.class})//拦截runtime异常
    public ResponseEntity<ExceptionResult> handlerException(LyException e){
        ExceptionEnum em = e.getExceptionEnum();
        return ResponseEntity.status(em.getCode()).body(new ExceptionResult(em));
    }
}
